package com.fz.us.admin.company.service.impl;

import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.base.bean.Result;
import com.fz.us.admin.base.bean.search.SearchEnum;
import com.fz.us.admin.base.bean.search.order.Order;
import com.fz.us.admin.base.bean.search.page.PageHelp;
import com.fz.us.admin.base.bean.search.page.PagePaging;
import com.fz.us.admin.base.bean.search.result.SearchResultList;
import com.fz.us.admin.base.bean.search.result.SearchResultPaging;
import com.fz.us.admin.base.bean.search.rule.Rule;
import com.fz.us.admin.base.bean.search.rule.RuleHelper;
import com.fz.us.admin.base.dao.BaseDao;
import com.fz.us.admin.base.service.impl.BaseServiceImpl;
import com.fz.us.admin.company.entity.Company;
import com.fz.us.admin.company.entity.Post;
import com.fz.us.admin.company.service.DutyService;
import com.fz.us.admin.company.service.PostService;
import com.fz.us.admin.company.entity.User;
import com.fz.us.admin.company.dao.PostDao;
import com.fz.us.admin.company.service.PowerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Service实现类 - 岗位
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-08-02 。
 * ============================================================================
 */
@Service
public class PostServiceImpl extends BaseServiceImpl<Post,String> implements PostService{

    @Resource
    private PostDao postDao;
    @Override
    public BaseDao<Post, String> getBaseDao() {
        return postDao;
    }

    @Resource
    private PowerService powerService;
    @Resource
    private DutyService dutyService;





    @Override
    public Result createEntity(User user, Post post) {
        Assert.notNull(user, "user is required");
        Assert.notNull(post, "post is required");

        Company company = user.getCompany();
        if (company == null)
            return resultService.fail("操作人没有所属企业");


        // 设置所属企业
        post.setCompany(user.getCompany());

        return saveEntity(post);
    }

    @Override
    public Result updateEntity(User user, String keyId, Post post) {
        Assert.notNull(user, "user is required");
        Assert.hasText(keyId, "keyId is required");
        Assert.notNull(post, "post is required");

        Company company = user.getCompany();
        if (company == null)
            return resultService.fail("操作人没有所属企业");

        Post postTemp = get(keyId);


        postTemp.setName(post.getName());

        postTemp.setSortNo(post.getSortNo());
        postTemp.setDescription(post.getDescription());

        update(postTemp);
        return resultService.successWithId(postTemp.getId());
    }

    @Override
    public Result methodEntity(User user, String method, String keyId) {
        Assert.notNull(user, "user is required");
        Assert.hasText(method, "method is required");
        Assert.hasText(keyId, "keyId is required");

        Post post = get(keyId);
        if (post != null) {
            if (StringUtils.equals("Delete", method)) {// 删除
                // 是否已配置职权
                if(powerService.hasPost(post)) {
                    return resultService.fail("岗位已配置部门职权,请先删除该职权");
                }

                return methodEntity(BaseEnumManager.MethodEnum.Delete, keyId);
            } else if (StringUtils.equals("Enable", method)) {// 启用
                return methodEntity(BaseEnumManager.MethodEnum.Enable, keyId);
            } else if (StringUtils.equals("Disenable", method)) {// 禁用
                return methodEntity(BaseEnumManager.MethodEnum.Disenable, keyId);
            }
        }

        return resultService.fail();
    }

    @Override
    public SearchResultPaging pagingPost(PagePaging page, List<Rule> rules, Order order, Company company, BaseEnumManager.StateEnum... states) {
        Assert.notNull(company, "company is required");

        if (rules == null)
            rules = new ArrayList<Rule>();
        rules.add(RuleHelper.eq("company", company));
        if (states != null && states.length > 0) {
            rules.add(RuleHelper.in("state", states));
        }

        // 默认排序
        if (order == null)
            order = Order.order("sortNo", SearchEnum.OrderType.asc);

        return findPagingBySearch(page, rules, order);
    }

    @Override
    public SearchResultList listPost(List<Rule> rules, Order order, Company company, BaseEnumManager.StateEnum... states) {
        Assert.notNull(company, "company is required");

        if (rules == null)
            rules = new ArrayList<Rule>();
        rules.add(RuleHelper.eq("company", company));
        if (states != null && states.length > 0) {
            rules.add(RuleHelper.in("state", states));
        }

        // 默认排序
        if (order == null)
            order = Order.order("sortNo", SearchEnum.OrderType.asc);

        return findListBySearch(rules, order);
    }

    @Override
    public Map<String, Object> getListMap(Post post) {
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("id", StringUtils.isEmpty(post.getId())?"":post.getId());
        map.put("name", StringUtils.isEmpty(post.getName())?"":post.getName());
        map.put("pinYinHead", StringUtils.isEmpty(post.getPinYinHead())?"":post.getPinYinHead().toLowerCase());
        map.put("pinYin", StringUtils.isEmpty(post.getPinYin())?"":post.getPinYin());

        map.put("sortNo", post.getSortNo());
        map.put("description", StringUtils.isEmpty(post.getDescription())?"":post.getDescription());

        // other
        map.put("state", post.getState()==null?"":post.getState().name());

        return map;
    }

    @Override
    public Map<String, Object> getDetailMap(Post post) {
        return getListMap(post);
    }

}
